/***************************************************************************
 *   This file is a part of match_mail.                               *
 *   Copyright (C) 2009 by fu4ny 										   *
 *   ntcong.it@gmail.com                                                   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 3 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *  You should have received a copy of the GNU General Public License      *
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>   *             *
 ***************************************************************************/

#include "Mail.h"
#include <sstream>
#include <cstring>
#include <iostream>
using namespace std;
Mail::Mail( const char* newFile ) {
	mailer = newFile;
	hasFrom = false;
	hasAttachment = false;
	hasSubject = false;
	hasId = false;
}

Mail::~Mail() {
}

string Mail::from() {
	ifs.open( mailer.c_str() );
	string mailFrom;
	string line;
	string token;
	while ( ifs.good() && !hasFrom ) {
		getline(ifs,line);
		istringstream iss(line);
		while ( iss.good() ) {
			iss >> token;
			if ( !token.compare("Return-Path:") ) {
				iss >> token;
				hasFrom = true;
				break;
			}
		}
	}
	if ( hasFrom ) mailFrom = token.substr(1, token.length()-2);
	ifs.close();
	return mailFrom;
}

bool Mail::isImage( const string arg1 ) {
	if ( !arg1.compare("jpg") || !arg1.compare("jpeg") ||
			!arg1.compare("bmp") )
		return true;
	return false;
}

string Mail::attachment(){
	ifs.open( mailer.c_str() );
	string attachment;
	string line;
	do {
		while ( ifs.good() ) {
			getline(ifs,line);
			if ( line.find("Content-Disposition:") != line.npos &&
					( ( line.find("attachment;") != line.npos ) || ( line.find("inline;") != line.npos ) ) ) {
				if ( line.find("filename=") == line.npos ) {
					getline( ifs,line );

				}
				break;
			}
		}
		if ( !ifs.good() ) {
			hasAttachment = false;
			return attachment;
		}
		attachment = line.substr( line.find("filename=")+10 );
		attachment.erase( attachment.end()-1 );
	} while ( !isImage( attachment.substr( attachment.find_last_of(".")+1 ) ) );
	ifs.close();
	hasAttachment = true;
	return attachment;
}


string Mail::subject() {
	ifs.open( mailer.c_str() );
		string subject;
		string line;
		string token;
		while ( ifs.good() && !hasSubject ) {
			getline(ifs,line);
			istringstream iss(line);
			while ( iss.good() ) {
				iss >> token;
				if ( !token.compare("Subject:") ) {
					cout << line << endl;
					hasSubject = true;
					break;
				}
			}
		}
		if ( hasSubject ) subject = line.substr(8);
		ifs.close();
		return subject;
}

string Mail::messageId(){
	ifs.open( mailer.c_str() );
		string mId;
		string line;
		string token;
		while ( ifs.good() && !hasId ) {
			getline(ifs,line);
			istringstream iss(line);
			while ( iss.good() ) {
				iss >> token;
				if ( !token.compare("Message-ID:") ) {
					iss >> token;
					hasId = true;
					break;
				}
			}
		}
		if ( hasId ) mId = token.substr(1, token.length()-2);
		ifs.close();
		return mId;
}
